<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/21
 * Time: 21:17
 */

namespace Home\Controller;
use Home\Common\Controller\LoginedController;

class CarController extends LoginedController
{
     public function lists(){
         if(IS_POST){
            dd($_POST);die;
         }
         $uid=session('uid');
//         dd($uid);
         $goods=$this->oncar($uid);
         $this->assign('goods',$goods);
//         dd($goods);
         $this->display();
     }
 //查找某个用户在在购物车中的商品
    public function oncar($uid){
//        dd($uid);die;
       return M('usergoods')
           ->join('sp_goods ON sp_usergoods.gid = sp_goods.gid')
           ->join("sp_product ON sp_usergoods.pcid=sp_product.pcid")
               ->where("sp_usergoods.uid=$uid AND sp_usergoods.gstatus=0")
           ->field("sp_goods.gname,sp_product.stock,sp_goods.gid,sp_goods.thumb,sp_goods.priced,sp_usergoods.count,sp_usergoods.ugid,sp_product.pcid,sp_product.style as pstyle")
               ->select();
    }
    //异步查找某个用户购物车中的商品
    public function finAjxgood(){
        if(IS_AJAX){
            $uid=I('post.uid');
//            dd($uid);die;
           $goods=$this->oncar($uid);

//            dd($goods);die;
            $this->ajaxReturn($goods);
        }
    }
    /**
     * 将商品加入到购物车
     */
    public function  addcar(){
        $uid=session('uid');
        $gid=I('get.gid');
        $pcid=I('get.pcid');
        $count=I('get.count');
//        dd($count);die;
//        dd($_GET);die;
        $_GET['uid']=$uid;
        $_GET['buytime']=time();
        $res=D('usergoods')->addcar(I('get.'));
        if($res){
            $this->redirect("home/car/lists/uid/{$uid}");die;
        }else{
            $this->error('购买失败');die;
        }
    }
    /**
     * 删除商品
     */
   public function delgood(){
        $gid=I('post.gid');
        $pcid=I('post.pcid');
         M('usergoods')->where("gid=$gid AND pcid=$pcid")->delete();
   }
    /**
     * 异步增加用户购物车中的商品的数量
     */
    public function ajaxAddgood(){
        if(IS_AJAX){
//            dd($_POST);die;
            $uid=session('uid');
            $gid=I('post.gid');
            $pcid=I('post.pcid');
           $res=M('usergoods')->where("uid=$uid AND gid={$gid} AND pcid={$pcid}")->setInc('count');
        }
    }

    /**
     * 异步减少用户购物车中的商品数量
     */
    public function  ajaxreduce(){
        if(IS_AJAX){
            $uid=session('uid');
            $gid=I('post.gid');
            $pcid=I('post.pcid');
            $res=M('usergoods')->where("uid=$uid AND gid={$gid} AND pcid={$pcid}")->setDec('count');
        }
    }

    /**
     * 异步删除购物车中的商品
     */
    public  function ajaxdelgood(){
        if(IS_AJAX){
//            dd(I('post.ugid'));die;
            $ugid=I('post.ugid');
            $res=M('usergoods')->where("ugid=$ugid")->delete();
            if($res){
                $this->ajaxReturn(['valid'=>1,'msg'=>'删除成功']);
            }else{
                $this->ajaxReturn(['valid'=>0,'msg'=>'删除失败']);
            }

        }
    }

}